package plenprojects.chatapi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.client.RestTemplate;

public class ChatRoom {
	
	private final String room;
	private final Session owner;
	private String lastCheck = convertDate(new Date());
	
	public ChatRoom(String room, Session owner) {
		
		this.room = room;
		this.owner = owner;
	}
	
	public void postMessage(String message) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		
		ServerMessage req = new ServerMessage();
		req.setParameter("message", message);
		
		String suffix = String.format(ChatContext.postMessageSuffix, owner.getToken(), room);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.postForObject(owner.getRootUrl() + suffix, req, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ChatMessage> getRecentMessages() throws ChatException, ConnectionException {
		
		String newCheck = convertDate(new Date());
		
		RestTemplate rest = new RestTemplate();
		
		String suffix = String.format(ChatContext.recentMessagesSuffix, owner.getToken(), room, lastCheck);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.getForObject(owner.getRootUrl() + suffix, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
		
		List<Map<String, String>> rawMessages = (List<Map<String, String>>)resp.getParameter("messages");
		
		List<ChatMessage> messages = new ArrayList<ChatMessage>();
		
		for(Map<String, String> map : rawMessages) {
			
			try {
				
				messages.add(ChatMessage.fromMap(map));
			}
			catch(ConversionException e) {
				
				throw new ChatException(e);
			}
		}
		
		lastCheck = newCheck;
		
		return messages;
	}
	
	public void inviteUser(String username) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		
		String suffix = String.format(ChatContext.inviteToRoomSuffix, owner.getToken(), room, username);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.postForObject(owner.getRootUrl() + suffix, new ServerMessage(), ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
	}
	
	public void kickUser(String username) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		
		String suffix = String.format(ChatContext.kickFromRoomSuffix, owner.getToken(), room, username);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.getForObject(owner.getRootUrl() + suffix, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
	}
	
	private String convertDate(Date date) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return sdf.format(date);
	}
}
